package com.wang.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.article.api.model.dto.ArticleDto;
import com.wang.article.api.model.pojo.ApArticle;
import com.wang.article.api.model.pojo.ApArticleConfig;
import com.wang.article.api.model.pojo.ApArticleContent;
import com.wang.article.mapper.ApArticleConfigMapper;
import com.wang.article.mapper.ApArticleContentMapper;
import com.wang.article.mapper.ArticleMapper;
import com.wang.article.service.IArticleService;
import com.wang.common.constants.ArticleConstants;
import com.wang.article.api.model.dto.ArticleHomeDto;
import com.wang.common.model.dto.ResponseResult;
import com.wang.common.model.enums.AppHttpCodeEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 加载新闻的业务处理类
 *
 * @author: wangning
 * @date: 2023/12/28 16:10
 */
@Service
@Transactional
@Slf4j
@RequiredArgsConstructor
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ApArticle> implements IArticleService {

    private final ApArticleConfigMapper apArticleConfigMapper;
    private final ApArticleContentMapper apArticleContentMapper;

    /**
     * 分页查询新闻
     * 每页展示数量 min -> 10,max -> 50
     *
     * @param loadtype 查询类型 1->加载更多，2->加载最新，all->默认
     * @param dto 前端传参
     * @return List<ApArticle> apArticles
     */
    @Override
    public ResponseResult load(Short loadtype, ArticleHomeDto dto) {

        Integer size = dto.getSize();
        if (size == null || size == 0) {
            size = 10;
        }
        size = Math.min(size, ArticleConstants.MAX_PAGE_SIZE);
        dto.setSize(size);
        if(!loadtype.equals(ArticleConstants.LOADTYPE_LOAD_MORE)&&!loadtype.equals(ArticleConstants.LOADTYPE_LOAD_NEW)){
            loadtype = ArticleConstants.LOADTYPE_LOAD_MORE;
        }
        //文章频道校验
        if(StringUtils.isEmpty(dto.getTag())){
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        //时间校验
        if(dto.getMaxBehotTime() == null) dto.setMaxBehotTime(new Date());
        if(dto.getMinBehotTime() == null) dto.setMinBehotTime(new Date());
        //2.查询数据
        List<ApArticle> apArticles = baseMapper.loadArticleList(dto, loadtype);
        //3.结果封装
        ResponseResult responseResult = ResponseResult.okResult(apArticles);
        return responseResult;
    }

    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        //1.检查参数
        if(dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto,apArticle);
        //2.判断是否存在id
        if(dto.getId() == null){
            //2.1 不存在id  保存  文章  文章配置  文章内容
            //保存文章
            save(apArticle);
            //保存配置
            ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
            apArticleConfigMapper.insert(apArticleConfig);
            //保存 文章内容
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.insert(apArticleContent);
        }else {
            //2.2 存在id   修改  文章  文章内容
            //修改  文章
            updateById(apArticle);
            //修改文章内容
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getId()));
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.updateById(apArticleContent);
        }
        //3.结果返回  文章的id
        return ResponseResult.okResult(apArticle.getId());
    }

}
